package com.imooc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import com.imooc.Interceptor.JwtCurrentUserInterceptor;
import com.imooc.base.BaseInfoProperties;
import com.imooc.bo.EditJobBO;
import com.imooc.bo.SearchJobsBO;
import com.imooc.enums.JobStatus;
import com.imooc.pojo.Job;
import com.imooc.pojo.Users;
import com.imooc.result.GraceJSONResult;
import com.imooc.service.JobService;
import com.imooc.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * @author liansen
 * @create 05-23-16:29
 */
@RestController
@RequestMapping("/job")
public class JobController extends BaseInfoProperties {

    @Autowired
    private JobService jobService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * HR发布职位 (新增/编辑)
     * @param editJobBO
     * @return
     */
    @PostMapping("/modify")
    public GraceJSONResult modify(@RequestBody @Valid EditJobBO editJobBO){
        Job job = new Job();
        BeanUtils.copyProperties(editJobBO, job);
        job.setStatus(JobStatus.OPEN.type);
        job.setUpdatedTime(LocalDateTime.now());
        if(StringUtils.isBlank(editJobBO.getId())){
            //新增
            LambdaQueryWrapper<Job> wrapper = new LambdaQueryWrapper<Job>()
                    .eq(Job::getId, editJobBO.getId())
                    .eq(Job::getCompanyId, editJobBO.getCompanyId())
                    .eq(Job::getJobName, editJobBO.getJobName());
            long count = jobService.count(wrapper);
            if(count > 0){
                return GraceJSONResult.errorMsg("该职位已经发布了");
            }
            job.setCreateTime(LocalDateTime.now());
            jobService.save(job);
        }else {
            //更新
            LambdaQueryWrapper<Job> wrapper = new LambdaQueryWrapper<Job>()
                    .eq(Job::getId, editJobBO.getId())
                    .eq(Job::getCompanyId, editJobBO.getCompanyId())
                    .eq(Job::getHrId, editJobBO.getHrId());

            jobService.update(job, wrapper);
        }
        redisTemplate.delete(REDIS_JOB_DETAIL + ":" + editJobBO.getCompanyId()
                            + ":" + editJobBO.getHrId() + ":" + editJobBO.getId());
        return GraceJSONResult.ok();
    }

    /**
     * app-hr端列表分页查询
     */
    @PostMapping("/hr/jobList")
    public GraceJSONResult hrJobList(String hrId, String companyId, Integer page, Integer limit, Integer status){

        if(StringUtils.isBlank(companyId)){
            return GraceJSONResult.error();
        }

        if(page == null) page = 1;
        if(limit == null) limit = 10;

        PagedGridResult result = jobService.queryJobList(hrId, companyId, page, limit, status);

        return GraceJSONResult.ok(result);
    }

    /**
     * 企业端hr查询职位列表
     */
    @PostMapping("/jobList")
    public GraceJSONResult JobList(Integer page, Integer limit){

        if(page == null) page = 1;
        if(limit == null) limit = 10;

        Users user = JwtCurrentUserInterceptor.currentUser.get();
        String companyId = user.getHrInWhichCompanyId();
        PagedGridResult result = jobService.queryJobList(null, companyId, page, limit, null);

        return GraceJSONResult.ok(result);
    }

    /**
     * HR查询职位详情
     */

    @PostMapping("/hr/jobDetail")
    public GraceJSONResult hrJobDetail(String jobId, String hrId, String companyId){
        String jobDetailJson = redisTemplate.opsForValue().get(REDIS_JOB_DETAIL + ":" + companyId + ":" + hrId + ":" + jobId);
        Job job;
        if(StringUtils.isNotBlank(jobDetailJson)){
            job = new Gson().fromJson(jobDetailJson, Job.class);
        }else {
            job = jobService.queryJobDetail(jobId, hrId, companyId);
            redisTemplate.opsForValue().set(REDIS_JOB_DETAIL + ":" + companyId + ":" + hrId + ":" + jobId, new Gson().toJson(job));
        }
        return GraceJSONResult.ok(job);
    }

    /**
     * 企业端hr查看职位详情
     * @param jobId
     * @return
     */
    @PostMapping("/admin/jobDetail")
    public GraceJSONResult adminJobDetail(String jobId){
        Job job = jobService.queryJobDetail(jobId, null, null);
        return GraceJSONResult.ok(job);
    }

    /**
     * HR关闭职位
     */
    @PostMapping("/close")
    public GraceJSONResult closeJob(String jobId, String hrId, String companyId){
        jobService.modifyJobStatus(jobId, hrId, companyId, JobStatus.CLOSE);
        redisTemplate.delete(REDIS_JOB_DETAIL + ":" + companyId + ":" + hrId + ":" + jobId);
        return GraceJSONResult.ok();
    }

    @PostMapping("/open")
    public GraceJSONResult openJob(String jobId, String hrId, String companyId){
        jobService.modifyJobStatus(jobId, hrId, companyId, JobStatus.OPEN);
        redisTemplate.delete(REDIS_JOB_DETAIL + ":" + companyId + ":" + hrId + ":" + jobId);
        return GraceJSONResult.ok();
    }

    /**
     * 普通用户搜索职位  (之后用es优化)
     */
    @RequestMapping("/searchJobs")
    public GraceJSONResult searchJobs(@RequestBody SearchJobsBO searchJobsBO, Integer page, Integer limit){
        PagedGridResult result = jobService.searchJobs(searchJobsBO, page, limit);
        return GraceJSONResult.ok(result);
    }



}
